package com.loong.member.handler;

import com.loong.common.constant.SingletonPager;
import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.utils.TransformUtils;
import com.loong.member.business.MemberAddressBusiness;
import com.loong.member.common.forehead.param.MemberAddressDetailQueryForeheadParam;
import com.loong.member.common.forehead.param.MemberAddressForeheadParam;
import com.loong.member.common.forehead.param.MemberAddressQueryForeheadParam;
import com.loong.member.common.forehead.result.MemberAddressForeheadResult;
import com.loong.member.dto.MemberAddressDTOResult;
import com.loong.member.model.MemberAddress;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.xml.crypto.dsig.Transform;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @Description: 处理会员地址业务的handler
 * @Author: LiuQun
 * @Date: 2020/12/25 18:10
 */
@Service
public class MemberAddressHandler {

    @Resource
    private MemberAddressBusiness addressBusiness;

    /**
     * 前台新增会员收货地址
     * @param param 收货地址参数
     */
    public void foreheadSaveMemberAddress(MemberAddressForeheadParam param){
        //根据用户id查询用户是否有其他收货地址，若没有则将该地址设置为默认地址
        List<MemberAddress> addressList = addressBusiness.findAllBy("userId", param.getUserId());
        //设置默认地址参数不让前端传，而是单独调用接口
        if (CollectionUtils.isEmpty(addressList)) param.setDefaultYes(true);
        param.setCreateTime(new Date());
        addressBusiness.save(TransformUtils.transform(param, MemberAddress.class));
    }

    /**
     * 前台更新会员收货地址
     * @param param 收货地址参数
     */
    public void foreheadUpdateMemberAddress(MemberAddressForeheadParam param){
        param.setLastUpdateTime(new Date());
        //更新地址时，不让设置默认地址。设置默认地址单独调用接口
        param.setDefaultYes(false);
        addressBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param, MemberAddress.class));
    }

    /**
     * 前台设置默认会员地址
     * @param addressId 会员地址id
     * @param userId 用户id
     */
    @Transactional
    public void foreheadUpdateDefaultMemberAddress(Long addressId,Long userId){
        //更新用户的所有地址都为非默认地址
        addressBusiness.updateMemberAddressNotDefaultByUserId(userId);
        //更新用户的指定地址为默认地址
        MemberAddressForeheadParam param = MemberAddressForeheadParam.builder()
                .id(addressId)
                .defaultYes(true)
                .build();
        addressBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param,MemberAddress.class));
    }

    /**
     * 前台删除会员收货地址
     * @param id 会员收货地址id
     */
    public void foreheadDeleteMemberAddress(Long id){
        addressBusiness.deleteById(id);
    }

    /**
     * 前台分页查询会员地址
     * @param queryParam 分页参数
     * @param userId 用户id
     * @return 分页结果
     */
    public Pager<MemberAddressForeheadResult> foreheadPageQueryMemberAddress(BaseQuery queryParam,Long userId){
        Pager<MemberAddress> pager = addressBusiness.pageQuery(queryParam, () -> addressBusiness.findAllBy("userId", userId));
        if (CollectionUtils.isEmpty(pager.getDataList())) return SingletonPager.getInstance();
        //默认收货地址排在前面
        pager.getDataList().sort(Comparator.comparing(MemberAddress::getDefaultYes).reversed());
        return TransformUtils.transformPage(pager,MemberAddressForeheadResult.class);
    }

    /**
     * 前台根据用户id查询用户的默认地址
     * @param userId 用户id
     * @return 会员地址结果
     */
    public MemberAddressForeheadResult foreheadFindDefaultMemberAddressByUserId(Long userId){
        List<MemberAddress> addressList = addressBusiness.findDefaultMemberAddressByUserId(userId);
        if (CollectionUtils.isEmpty(addressList)) return null;
        return TransformUtils.transform(addressList.get(0),MemberAddressForeheadResult.class);
    }

    /**
     * 前台根据参数查询会员地址详情
     * @param param 会员地址id和用户id封装的参数
     * @return 会员地址
     */
    public MemberAddressForeheadResult foreheadFindMemberAddressByParam(MemberAddressDetailQueryForeheadParam param){
        List<MemberAddress> addressList = addressBusiness.findMemberAddressDetailByParam(param);
        if (CollectionUtils.isEmpty(addressList)) return new MemberAddressForeheadResult();
        return TransformUtils.transform(addressList.get(0),MemberAddressForeheadResult.class);
    }

    /**
     * RPC接口：根据地址id查询会员地址
     * @param addressId 地址id
     * @return 会员地址结果
     */
    public MemberAddressDTOResult findMemberAddressById(Long addressId){
        MemberAddress address = addressBusiness.findById(addressId);
        if (ObjectUtils.isEmpty(address)) return null;
        return TransformUtils.transform(address,MemberAddressDTOResult.class);
    }
}
